package com.kzj.global.utils;

import com.kzj.domain.*;
import com.kzj.global.constant.GlobalConstant;
import com.kzj.global.exception.MyException;
import com.kzj.global.exception.ParamsClassException;
import nonapi.io.github.classgraph.fileslice.ArraySlice;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MatchesUtil {
    public static void idMatches(String id)
    {
        if(id==null)
            throw new MyException(GlobalConstant.ID_NO_NULL);
        if(id.length()!=GlobalConstant.ID_LEN)
            throw new MyException(GlobalConstant.ID_ERROR);
        String head=id.substring(0,1);
        String[] allowHead={GlobalConstant.STUDENT_PREFIX,GlobalConstant.TEACHER_PREFIX,
                GlobalConstant.ADMINISTRATOR_PREFIX,GlobalConstant.COURSE_INFORMATION_PREFIX,
                GlobalConstant.ELECTIVE_COURSE_PREFIX,GlobalConstant.ELECTIVE_COURSE_PREFIX,
                GlobalConstant.CLASSROOM_PREFIX,GlobalConstant.COURSE_INFORMATION_PREFIX,
        GlobalConstant.MAJOR_PREFIX,GlobalConstant.ACADEMY_PREFIX,GlobalConstant.STUDENT_CLASS_PREFIX};
        List<String> list = Arrays.asList(allowHead);
        if(!list.contains(head))
        {
            throw new MyException(GlobalConstant.ID_ERROR);
        }
        //TODO id 正则表达式 失败 throw new MyException()

    }
    public static void idMatches(String id,Class aClass)
    {
        idMatches(id);
    }
    public static Class idType(String id)
    {
        idMatches(id);
        String tag=id.substring(0,1);
        Class<String> stringClass = String.class;
        Map<String,Class> map=new HashMap<String,Class>(){{
            put(GlobalConstant.STUDENT_PREFIX, Student.class);
            put(GlobalConstant.TEACHER_PREFIX, Teacher.class);
            put(GlobalConstant.ADMINISTRATOR_PREFIX, Administrator.class);
        }};
        return map.get(tag);
    }
    public static void objMatches(Object obj)
    {
        if(obj==null)
            throw new MyException(GlobalConstant.PARAMS_NULL);
        if(obj instanceof Student)
        {
            studentMatches((Student) obj);
        }
        else if(obj instanceof Teacher)
        {
            teacherMatches((Teacher) obj);
        }
        else if(obj instanceof Administrator)
        {
            administratorMatches((Administrator) obj);
        }
        else if(obj instanceof ElectiveCourse)
        {
            electiveCourseMatches((ElectiveCourse) obj);
        }
        else if(obj instanceof CourseSelectionInformation)
        {
            courseSelectionInformationMatches((CourseSelectionInformation) obj);
        }
        else{
            throw new ParamsClassException();
        }
    }

    private static void courseSelectionInformationMatches(CourseSelectionInformation obj) {
    }

    private static void electiveCourseMatches(ElectiveCourse obj) {
        if(obj.getTotalAmount()==null||obj.getTotalAmount()<0)
            throw new MyException(GlobalConstant.PARAMS_ID_NUMBER_ERROR);
        if(obj.getAmount()!=null&&obj.getAmount()<0)
            throw new MyException(GlobalConstant.PARAMS_ID_NUMBER_ERROR);
    }

    public static void studentMatches(Student student){
        if(student.getIdNumber()!=null)
        {
            idNumberMatches(student.getIdNumber());
        }
        if (student.getPhone()!=null)
        {
            phoneMatches(student.getPhone());
        }
        if(student.getSex()!=null)
        {
            sexMatchers(student.getSex());
        }
        if(student.getMajorId()!=null)
        {
            //TODO 修改id
            idMatches(student.getMajorId());
        }
        if(student.getStudentClassId()!=null)
        {
            idMatches(student.getStudentClassId());
        }

    }
    public static void teacherMatches(Teacher teacher){
        if(teacher.getIdNumber()!=null)
        {
            idNumberMatches(teacher.getIdNumber());
        }
        if (teacher.getPhone()!=null)
        {
            phoneMatches(teacher.getPhone());
        }
        if(teacher.getSex()!=null)
        {
            sexMatchers(teacher.getSex());
        }
    }
    public static void administratorMatches(Administrator administrator){
        if(administrator.getIdNumber()!=null)
        {
            idNumberMatches(administrator.getIdNumber());
        }
        if (administrator.getPhone()!=null)
        {
            phoneMatches(administrator.getPhone());
        }
        if(administrator.getSex()!=null)
        {
            sexMatchers(administrator.getSex());
        }
    }

    public static void idNumberMatches(String idNumber)
    {
        if(!idNumber.matches(GlobalConstant.ID_NUMBER_REGULAR_EXPRESSION))
            throw new MyException(GlobalConstant.PARAMS_ID_NUMBER_ERROR);
    }
    public static void phoneMatches(String phone)
    {
        if(!phone.matches(GlobalConstant.PHONE_REGULAR_EXPRESSION))
            throw new MyException(GlobalConstant.PARAMS_PHONE_ERROR);
    }
    public static void sexMatchers(String sex)
    {
        if(!sex.equals("男")&&!sex.equals("女"))
            throw new MyException(GlobalConstant.PARAMS_SEX_ERROR);
    }
    public static void passwordMatchers(String password)
    {

    }
    public static void termMatchers(Integer term)
    {
        if(term==null||term<=0||term>8)
            throw new MyException(GlobalConstant.PARAMS_TERM_ERROR);
    }


}
